package org.shiki.hrm.controller;

import org.shiki.hrm.doc.CourseDoc;
import org.shiki.hrm.dto.CourseDto;
import org.shiki.hrm.query.CourseDocQuery;
import org.shiki.hrm.service.ICourseService;
import org.shiki.hrm.domain.Course;
import org.shiki.hrm.query.CourseQuery;
import org.shiki.hrm.util.AjaxResult;
import org.shiki.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
	@Autowired
	public ICourseService courseService;


	/**
	 * 保存和修改公用的
	 *
	 * @param course 传递的实体
	 * @return Ajaxresult转换结果
	 */
	@PutMapping
	@PreAuthorize("hasAuthority('course:add') or hasAuthority('course:update')")
	public AjaxResult addOrUpdate(@RequestBody Course course) {
		try {
			if (course.getId() != null)
				courseService.updateById(course);
			else
				courseService.insert(course);
			return AjaxResult.me();
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
		}
	}

	/**
	 * 删除对象信息
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/{id}")
	@PreAuthorize("hasAuthority('course:delete')")
	public AjaxResult delete(@PathVariable("id") Long id) {
		try {
			courseService.deleteById(id);
			return AjaxResult.me();
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
		}
	}

	//获取用户
	@GetMapping("/{id}")
	public Course get(@PathVariable("id") Long id) {
		return courseService.selectById(id);
	}

	/**
	 * 查看所有的员工信息
	 *
	 * @return
	 */
	@GetMapping()
	public List<Course> list() {

		return courseService.selectList(null);
	}


	/**
	 * 分页查询数据
	 *
	 * @param query 查询对象
	 * @return PageList 分页对象
	 */
	@PostMapping("/list")
	public PageList<Course> json(@RequestBody CourseQuery query) {
		Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
		page = courseService.selectPage(page);
		return new PageList<Course>(page.getTotal(), page.getRecords());
	}

	@PostMapping("/save")
	public AjaxResult saveCourse(@RequestBody CourseDto dto) {
		return courseService.saveCourse(dto);
	}

	@PostMapping("/onLineCourse")
	@PreAuthorize("hasAuthority('course:onLineCourse')")
	public AjaxResult onLineCourse(@RequestBody List<Long> ids) {
		return courseService.onLineCourse(ids);
	}

	@PostMapping("/offLineCourse")
	@PreAuthorize("hasAuthority('course:offLineCourse')")
	public AjaxResult offLineCourse(@RequestBody List<Long> ids) {
		return courseService.offLineCourse(ids);
	}

    @PostMapping("/queryCourses")
    public AjaxResult queryCourses(@RequestBody CourseDocQuery query) {
	    return courseService.queryCourses(query);
    }

}
